package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Merchant;
import com.tjetc.service.MerchantAccountService;
import com.tjetc.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

//控制商家账户相关信息
@RestController
public class MerchantAccountController {

    @Autowired
    private MerchantAccountService merchantAccountService;

    //登录功能
    @RequestMapping("merchant/login")
    public JsonResult login(@RequestParam("username") String phone,
                            @RequestParam("password") String password,
                            HttpSession session) {
        JsonResult result = merchantAccountService.login(phone, password);
        if (result.getState() == 0) {
            session.setAttribute("id", ((Merchant) result.getData()).getId());
        }
        return result;
    }

    //注册功能
    @RequestMapping("merchant/register")
    public JsonResult register(@RequestParam("phone") String phone,
                               @RequestParam("pass") String password,
                               @RequestParam("name") String name) {
        return merchantAccountService.register(phone, password, name);
    }

    //退出登录功能
    @RequestMapping("merchant/logout")
    public JsonResult logout(HttpSession session) {
        session.invalidate();
        return new JsonResult(0, "登出成功", null);
    }

    //修改密码功能
    @RequestMapping("merchant/modifyPassword")
    public JsonResult modifyPassword(@RequestParam("oldPass") String oldPass,
                                     @RequestParam("pass") String newPass,
                                     HttpSession session) {
        Integer merchantId = (Integer) session.getAttribute("id");
        return merchantAccountService.modifyPassword(oldPass, newPass, merchantId);
    }


    //开通店铺功能
    @RequestMapping("merchant/setup")
    public JsonResult setup(@RequestBody Merchant merchant,
                            HttpSession session) {
        Integer merchantId = (Integer) session.getAttribute("id");
        System.out.println(merchant);
        return merchantAccountService.setUp(merchant, merchantId);
    }

    //获取店铺信息功能
    @RequestMapping("merchant/get")
    public JsonResult getMerchantMessage(@RequestParam("userId") String param) {
        return merchantAccountService.getMerchantMessage(param);
    }

    @RequestMapping("merchant/changeShopStatus")
    public JsonResult ChangeShopStatus(@RequestParam("st")String st,HttpSession session){
        Integer status= Integer.valueOf(st);
        Integer merchantId= (Integer) session.getAttribute("id");
        return new JsonResult(0,"修改成功",merchantAccountService.changeShopStatus(merchantId,status));
    }

    @RequestMapping("merchant/changeDropFee")
    public JsonResult changeDropFee(@RequestParam("dropFee")String dropOffFee,HttpSession session){
        BigDecimal dropFee=new BigDecimal(dropOffFee);
        Integer merchantId= (Integer) session.getAttribute("id");
        return merchantAccountService.changeDropFee(merchantId, dropFee);
    }

}
